package com.ninetaildemonfox.zdl.socialproject.http.https;

import com.hyphenate.easeui.utils.map.BaseUrl;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.jsoncover.JsonConverterFactory;
import com.ninetaildemonfox.zdl.socialproject.config.RetrofitInterface;
import com.orhanobut.logger.Logger;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/30
 * 功能描述：M层
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class Model {
    private String url = BaseUrl.BaseHttpUrl;
    private Retrofit retrofit;
    private RetrofitInterface userApi;

    private static boolean isDebug = true;
    private static Model bombClient;

    public static Model getInstance() {
        if (bombClient == null) {
            bombClient = new Model();
        }
        return bombClient;
    }

    private Model() {
//        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
//            @Override
//            public void log(String message) {
//                //打印retrofit日志
//                Logger.i("==http"+message);
//            }
//        });
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        //debug模式开启日志输出
        loggingInterceptor.setLevel(isDebug ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(JsonConverterFactory.create())
                .client(okHttpClient)
                .build();
    }


    public RetrofitInterface getUserApi() {
        if (userApi == null) {
            userApi = retrofit.create(RetrofitInterface.class);
        }
        return userApi;
    }
}
